package com.cloudshadow.platform.server.common.uc.business.service;

import com.alibaba.fastjson.JSONObject;
import com.cloudshadow.platform.assembly.security.ClientSession;
import com.cloudshadow.platform.assembly.security.SessionService;
import com.cloudshadow.platform.assembly.security.model.AccessTokenModel;
import com.cloudshadow.platform.assembly.security.model.ClientModelBase;
import com.cloudshadow.platform.assembly.security.model.Principal;
import com.cloudshadow.platform.common.util.MD5Util;
import com.cloudshadow.platform.infrastructure.exception.ErrorInfos;
import com.cloudshadow.platform.server.common.base.annotation.NoLogin;
import com.cloudshadow.platform.server.common.base.model.PageModel;
import com.cloudshadow.platform.server.common.base.service.BaseServerService;
import com.cloudshadow.platform.server.common.base.utils.VoUtil;
import com.cloudshadow.platform.server.common.base.vo.BasicVo;
import com.cloudshadow.platform.server.common.base.vo.TokenVo;
import com.cloudshadow.platform.server.common.uc.business.dao.UserDao;
import com.cloudshadow.platform.server.common.uc.protocol.model.UserModel;
import com.cloudshadow.platform.server.common.uc.protocol.protocol.UcProtocol;
import com.cloudshadow.platform.server.common.uc.protocol.req.UserAddReq;
import com.cloudshadow.platform.server.common.uc.protocol.req.UserLoginReq;
import com.cloudshadow.platform.server.common.uc.protocol.req.UserSearchReq;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by apple on 17/3/3.
 */
@RestController
@RequestMapping("demo")
public class UserService extends BaseServerService<UserDao,UserModel> implements UcProtocol {
    @Autowired
    private SessionService sessionService;

    @NoLogin
    @HystrixCommand(fallbackMethod="getFallback")
    public BasicVo<UserModel> findAll(@RequestBody UserSearchReq searchReq){
        return VoUtil.setSuccessResult(
                new PageModel(
                        getPage(searchReq)
                )

        );
    }


    //    @HystrixCommand(fallbackMethod="getFallback")
    @NoLogin
    public BasicVo<UserModel> findOne(@RequestParam String id){
//        server.save(changeEntity(new DemoAddReq()));
//        server.delete(id);
        return VoUtil.setSuccessResult(findOneByID(id));
    }

    @NoLogin
    public BasicVo<UserModel> save(@RequestBody UserAddReq saveReq){
        return VoUtil.setSuccessResult(
                save(
                        changeEntity(saveReq)
                )
        );
    }

    public BasicVo<UserModel> del(@RequestParam String id){
        delete(id);
        return VoUtil.setSuccessResult();
    }

    @Override
    public BasicVo<TokenVo> login(@RequestBody UserLoginReq req) {
        UserModel userModel = new UserModel();
        userModel.setLogin_account(req.getLogin_account());
        userModel.setPassword(MD5Util.MD5(req.getPassword()));
        userModel = this.getOne(userModel);
        if (userModel == null) {
            return null;
        }
        Principal principal = getClientModel(userModel);
        if (principal == null) {
            return VoUtil.genErrorResult(ErrorInfos.ClientErr.LOGIN_ERR);
        }

        return VoUtil.setSuccessResult(
                saveIntoSession(principal)
        );
    }



    protected TokenVo saveIntoSession(ClientModelBase client) {
        ClientSession session = new ClientSession();
        session.setClient(JSONObject.parseObject(JSONObject.toJSONString(client)));
        session.setClientId(client.getClientIdStr());
        AccessTokenModel token = sessionService.renewToken(session);
        TokenVo tokenVo = new TokenVo();
        tokenVo.setAccess_token(token.getToken());
        tokenVo.setExpire(token.getExpire());
        return tokenVo;
    }

    private Principal getClientModel(UserModel user) {
        Principal model = new Principal();
        model.setId(user.getId());
        model.setUsername(user.getLogin_account());
        model.setUser(user);
        return model;
    }
}
